package auctionClient;

import java.security.Key;
import java.security.MessageDigest;

import com.sun.org.apache.bcel.internal.generic.NEW;


public class HMACVerifier {
	
	private Key key;
	private String input;
	
	public HMACVerifier(String input, Key key){
		this.input=input;
		this.key=key;
	}
	
	public boolean verify() throws java.lang.StringIndexOutOfBoundsException{
		String hashString=input.substring(input.indexOf("[HMAC]")+6);
		return verifyHash(hashString,input.substring(0, input.indexOf("[HMAC]")));
	}
	
	private boolean verifyHash(String hash, String content){
		byte[] computedHash;
		HMACCreator computer=new HMACCreator(content, key);
		computedHash=computer.createHashMac();
		String computed=new String(computedHash);
		computedHash=computed.getBytes();
		
		return MessageDigest.isEqual(computedHash,hash.getBytes());
	}
}
